﻿using System;
using System.Web;
using System.Web.Mvc;
using System.Web.UI;

namespace Lbb.WSK
{
    public static class ExtPage
    {
        #region InitMasterPageFile
        public static void InitAdminMasterPageFile(this MasterPage masterPage)
        {
            if (masterPage == null) return;
            var masterPageFileName = GetAdminMasterPagePath();
            if (!string.IsNullOrWhiteSpace(masterPageFileName)) masterPage.MasterPageFile = masterPageFileName;
        }

        public static void InitAdminMasterPageFile(this Page page)
        {
            if (page == null) return;
            var masterPageFileName = GetAdminMasterPagePath();
            if (!string.IsNullOrWhiteSpace(masterPageFileName)) page.MasterPageFile = masterPageFileName;
        }
        #endregion InitMasterPageFile

        #region Helpers
        private static string GetAdminMasterPagePath()
        {
            string masterPageFileName = null;
            if (HttpContext.Current != null)
            {
                var cache = HttpContext.Current.Cache;
                if (cache["cachedAdminMaster"] == null)
                {
                    var getter = UnityServer.Resolve<IGetMasterPage>("GetAdminMasterPage");
                    masterPageFileName = getter.GetMasterPagePath();
                    cache.Insert("cachedAdminMaster", masterPageFileName, null, DateTime.Now.AddSeconds(300), System.Web.Caching.Cache.NoSlidingExpiration);
                }
                else
                {
                    masterPageFileName = cache["cachedAdminMaster"] as string;
                }
            }
            return masterPageFileName;
        }
        #endregion Helpers

    }
    public class AdminViewMasterPage : ViewMasterPage
    {
        public AdminViewMasterPage()
        {
            this.InitAdminMasterPageFile();
        }
    }
    public class AdminViewMasterPage<TModel> : ViewMasterPage<TModel>
    {
        public AdminViewMasterPage()
        {
            this.InitAdminMasterPageFile();
        }
    }
}
